package com.universalInterface.lims_interface.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.universalInterface.common.result.ApiResult;
import com.universalInterface.lims_interface.service.IExternalDataApiService;
import com.universalInterface.lims_interface.vo.api.param.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Tag(name = "外部数据api")
@Validated
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/external/api")
public class ExternalDataApiController {

	private final IExternalDataApiService iExternalDataApiService;

	@Operation(summary = "创建实验室云空间")
	@PostMapping("/createSpace")
	public ApiResult createSpace(@Parameter(description = "参数对象") @Valid @RequestBody CreateSpaceParam param) {
		String s = iExternalDataApiService.createSpace(param);
		return ApiResult.success("创建成功！", s);
	}

	@Operation(summary = "创建实验室通道")
	@PostMapping("/createSpaceChannel")
	public ApiResult createSpaceChannel(@Parameter(description = "参数对象") @Valid @RequestBody CreateSpaceChannelParam param) {
		String s = iExternalDataApiService.createSpaceChannel(param);
		return ApiResult.success("创建成功！", s);
	}

	@Operation(summary = "获取实验室云空间编号")
	@PostMapping("/getTestNum")
	public ApiResult getTestNum(@Parameter(description = "参数对象") @Valid @RequestBody GetTestNumParam param) {
		String s = iExternalDataApiService.getTestNum(param);
		return ApiResult.success("查询成功！", s);
	}

	@Operation(summary = "修改实验室云空间配置")
	@PostMapping("/updateSpace")
	public ApiResult updateSpace(@Parameter(description = "参数对象") @Valid @RequestBody UpdateSpaceParam param) {
		String s = iExternalDataApiService.updateSpace(param);
		return ApiResult.success("修改成功！size = " + s);
	}

	@Operation(summary = "查询最新试验项目云空间ID号")
	@PostMapping("/getTestSpaceId")
	public ApiResult getTestId(@Parameter(description = "参数对象") @Valid @RequestBody GetTestIdParam param) {
		String s = iExternalDataApiService.getTestId(param);
		return ApiResult.success("查询成功！", s);
	}

	@Operation(summary = "更新实验云对应的在测试验项目的通道配置")
	@PostMapping("/testChannel")
	public ApiResult testChannel(@Parameter(description = "参数对象") @Valid @RequestBody TestChannelParam param) {
		String i = iExternalDataApiService.testChannel(param);
		return ApiResult.success("更新成功！size = " + i);
	}

	@Operation(summary = "更新实验云对应的在测试验项目的曲线轴配置")
	@PostMapping("/reviseTest")
	public ApiResult reviseTest(@Parameter(description = "参数对象") @Valid @RequestBody ReviseTestParam param) {
		String i = iExternalDataApiService.reviseTest(param);
		return ApiResult.success("更新成功！size = " + i);
	}

	@Operation(summary = "更新实验云对应的在测试验项目的测试数据")
	@PostMapping("/realTimeData")
	public ApiResult realTimeData(@Parameter(description = "参数对象") @Valid @RequestBody RealTimeDataParam param) {
		String i = iExternalDataApiService.realTimeData(param);
		return ApiResult.success("更新成功！size = " + i);
	}

	@Operation(summary = "停止实验云对应的在测试验项目的测试")
	@PostMapping("/stopTest")
	public ApiResult stopTest(@Parameter(description = "参数对象") @Valid @RequestBody StopTestParam param) {
		String i = iExternalDataApiService.stopTest(param);
		return ApiResult.success("停止成功！");
	}

	@Operation(summary = "创建试验空间")
	@PostMapping("/TestSpace")
	public ApiResult createTestSpace(@Parameter(description = "创建实验室云空间参数") @Valid @RequestBody TestSpaceParam param) {
		List<Object> s = iExternalDataApiService.createTestSpace(param);
		return ApiResult.success("创建成功！", s);
	}

	@Operation(summary = "实验云对应的在测试验项目的测试报告上传")
	@PostMapping("/reportUpload")
	public ApiResult reportUpload(@Parameter(description = "测试报告上传") @Valid @RequestBody ReportParam param) {
		String s = iExternalDataApiService.reportUpload(param);
		return ApiResult.success("创建成功！");

	}

	// @Operation(summary = "批量添加实验室下的实时数据")
	// @GetMapping("/saveBatchForTestData")
	public ApiResult saveBatchForTestData(@Parameter(description = "表名") @RequestParam("tableName") String tableName,
										  @Parameter(description = "json字符串") @RequestParam("json") String json) {
		log.info("JSON中保持字段顺序:{}", JSON.parseArray(json, Feature.OrderedField).toJSONString());
		// 保持字段顺序
		List<Map> javaList = JSON.parseArray(json, Map.class, Feature.OrderedField);
		Integer i = iExternalDataApiService.saveBatchForTestData(tableName, javaList);
		return ApiResult.success("更新成功！size = " + i);
	}

}
